#ifdef WIN32
#include <windows.h>
#endif

#include <cstdlib>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include "structure.h"
#include "headers.h"
#include "constantes.h"




  void initPositions (Position *P)
  {
  	  P->losePos.x = 290;
	  P->losePos.y = 330;
  	    /* la base */
	  P->matchPos [0].x  = 85;
	  P->matchPos [0].y  = 50;
	  P->matchPos [0].w  = 10;
	  P->matchPos [0].h  = 120;

	  for (size_t i = 1; i < 21;i++)
	  {
     	  P->matchPos [i].x  = P->matchPos[i-1].x + 31;
     	  P->matchPos [i].y  = 50;
     	  P->matchPos [i].w  = 10;
	       P->matchPos [i].h  = 120;
       }
  }






/* la fonction jouer */
void play_Nim (Surface *S, Tools *T , Position *P)
{
	while (T->remaining_Match > 0 && T->done)
     {
     	switch (T->passHand)
     	{
     		case 1:
			player   (T);
			T->passHand =2; /* tour de l'ordi */
			break;
			case 2:
     	     computer (T);
     	     T->passHand =1; /* tour du joueur*/
     	     SDL_Delay (200);
     	     break;
     	     default:
     	     break;
     	}

          if (T->remaining_Match <=1)
          T->nb_Remove_Match  =1; /* pour eviter un bug probable */
     	T->current 	    += T->nb_Remove_Match;
	     T->remaining_Match -= T->nb_Remove_Match;
	     removeMatch (S , P ,  T->current);
		SDL_Flip (S->screen);
     }

     if (T->remaining_Match <=1)
	check_Lose (T , S , P);  /*affiche : le joeur a perdu*/
}






/* supprimer allumettes */
  void removeMatch (Surface *S ,Position *P ,unsigned int nb_Remove_Match)
  {
  	for (size_t i = 0; i < nb_Remove_Match ;i++)
  	{
  		SDL_FillRect(S->screen, &P->matchPos[i],
  		SDL_MapRGB(S->screen->format, 0, 0, 0));
  	}
  }





void creatPlayEnv (Surface *S , Position *P)
{
	SDL_BlitSurface (S->fond , NULL ,S->screen , NULL);
     for (size_t i = 0 ; i < 21;i++)
     {
     	SDL_BlitSurface (S->match , NULL ,S->screen ,&P->matchPos[i]);
     }
}





 void initTools (Tools *T)
 {
 	T->done = true;
  	T->current  = 0;
  	T->passHand = 1;
     T->nb_Remove_Match =  0;
	T->remaining_Match = 21;
 }







void check_Lose (Tools *T , Surface *S , Position *P)
 {
 	SDL_SetAlpha(S->lose , SDL_SRCALPHA, 90);
	SDL_BlitSurface (S->lose , NULL ,S->screen ,&P->losePos);
	SDL_Flip (S->screen); /* un dernier flip en sortant de la boucle des events ,pour afficher que le joueur perdu */

	while (T->done)
	{
		SDL_WaitEvent(&T->event);
          switch(T->event.type)
		case SDL_QUIT: T->done = false;
	}
 }








  short creatWindow (Surface *win)
   {
   	if(SDL_Init(SDL_INIT_VIDEO)!= 0) return -1;
     win->screen = SDL_SetVideoMode
						    (HEIGHT ,
							WIDTH ,
		/* main window */		32 ,
							SDL_HWSURFACE|
							SDL_DOUBLEBUF
									   );
     if (win->screen == NULL) return -1;
     SDL_WM_SetCaption ("Allumettes", NULL);
     return 0;
   }





  void clean_up(Surface *S)
  {
	SDL_FreeSurface(S->lose);
  	SDL_FreeSurface(S->fond);
     SDL_FreeSurface(S->match);
	SDL_Quit();
  }





 short loadFiles (Surface *S)
 {
 	S->fond  = IMG_Load ("media\\Fond.png");
 	if (!S->fond)
 	{
 		#ifdef WIN32
		MessageBox(NULL, "Fichier Fond.png introuvable !","Allumettes",MB_OK);
		SDL_Quit();
		#endif
		exit (-1);
 	}

 	S->match = IMG_Load ("media\\allumette.png");
 	if (!S->match)
 	{
 		#ifdef WIN32
		MessageBox(NULL ,"Fichier allumette.png introuvable !","Allumettes",MB_OK);
		SDL_Quit();
		#endif
		exit (-1);
 	}

 	S->lose = IMG_Load ("media\\lose.png");
 	if (!S->lose)
 	{
 		#ifdef WIN32
		MessageBox(NULL ,"Fichier lose.png introuvable !","Allumettes",MB_OK);
		SDL_Quit();
		#endif
		exit (-1);
 	}

     return 0;
 }

